const glob = require("glob");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin"); //html页面
/**
 * 多页面打包入口文件和HTML页面配置获取 
 *
 * @returns entry,HTMLWebpackPlugins
 */
const setMPA = () => {
  const entry = {};//入口对象
  const HTMLWebpackPlugins = [];//多页面配置对象
  const entryFiles = glob.sync(path.join(__dirname, "../src/apps/*/index.tsx")); //获取src目录下面的入口文件
  Object.keys(entryFiles).map(index => {
    //遍历入口文件
    const entryFile = entryFiles[index]; //拿到入口文件路径
    const match = entryFile.match(/src\/apps\/(.*)\/index\.tsx/); //解析入口文件路径
    const pageName = match && match[1]; //拿到模块名称
    entry[pageName] = entryFile; //设置入口名称和路径

    // 增加多页面打包配置
    HTMLWebpackPlugins.push(
      new HtmlWebpackPlugin({
        title:pageName,
        inlineSource: ".css$",
        template: path.join(__dirname,  `../src/apps/${pageName}/index.html`),
        filename: `${pageName}.html`,
        chunks: ['vendor','common',pageName],
        inject: true,
        hash:true,
        cache:false,
        minify: {
          html5: true,
          collapseWhitespace: true,
          preserveLineBreaks: false,
          minifyCSS: true,
          minifyJS: true,
          removeComments: false
        }
      })
    );
  
  });

  return {
    entry,
    HTMLWebpackPlugins
  };
};

module.exports = setMPA;
